﻿using UnityEngine;
using UnityEngine.UI;

/*
 *    五维图--正五边形
                 1
           
          
         2               5
                 0
         
         
             3       4    
    
*/
//ref: http://gad.qq.com/article/detail/35340?ADTAG=gad.tg.sh

[RequireComponent(typeof(CanvasRenderer))]
public class RadarChart : Graphic
{
    private Vector2[] vertPos = new Vector2[6];

    //查表
    float sin72 = 0f;
    float cos72 = 0f;
    float sin54 = 0f;
    float cos54 = 0f;
    float sin36 = 0f;
    float cos36 = 0f;
    float sin18 = 0f;
    float cos18 = 0f;

    protected override void Start()
    {
        sin72 = Mathf.Sin(GetRadians(72));
        cos72 = Mathf.Cos(GetRadians(72));
        sin54 = Mathf.Sin(GetRadians(54));
        cos54 = Mathf.Cos(GetRadians(54));
        sin36 = Mathf.Sin(GetRadians(36));
        cos36 = Mathf.Cos(GetRadians(36));
        sin18 = Mathf.Sin(GetRadians(18));
        cos18 = Mathf.Cos(GetRadians(18));
    }

    private float GetRadians(float angle)
    {
        return Mathf.Deg2Rad * angle;
    }

    private float half_height = 50;
    //参数：百分占比
    public void SetVertValue(float p1 = 1.0f, float p2 = 1.0f, float p3 = 1.0f, float p4 = 1.0f, float p5 = 1.0f)
    {
        half_height = this.rectTransform.sizeDelta.x / 2;//正五边形，外接正方形边一半

        //顶点
        vertPos[0] = new Vector2(0, 0);//中心点不变
        vertPos[1] = new Vector2(0, half_height * p1);
        vertPos[2] = new Vector2(-half_height * sin72 * p2, half_height * cos72 * p2);
        vertPos[3] = new Vector2(-half_height * cos54 * p3, -half_height * sin54 * p3);
        vertPos[4] = new Vector2(half_height * sin36 * p4, -half_height * cos36 * p4);
        vertPos[5] = new Vector2(half_height * cos18 * p5, half_height * sin18 * p5);

        SetVerticesDirty();//刷新数据
    }

    //重绘UI的Mesh
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        DrawRadar(vh);
    }
    
    private void DrawRadar(VertexHelper vh)
    {
        Color32 color = this.color;
        vh.Clear();

        //顶点
        vh.AddVert(new Vector2(vertPos[0].x, vertPos[0].y), color, Vector2.zero);
        vh.AddVert(new Vector2(vertPos[1].x, vertPos[1].y), color, Vector2.zero);
        vh.AddVert(new Vector2(vertPos[2].x, vertPos[2].y), color, Vector2.zero);
        vh.AddVert(new Vector2(vertPos[3].x, vertPos[3].y), color, Vector2.zero);
        vh.AddVert(new Vector2(vertPos[4].x, vertPos[4].y), color, Vector2.zero);
        vh.AddVert(new Vector2(vertPos[5].x, vertPos[5].y), color, Vector2.zero);

        //三角形
        vh.AddTriangle(0, 1, 2);
        vh.AddTriangle(0, 2, 3);
        vh.AddTriangle(0, 3, 4);
        vh.AddTriangle(0, 4, 5);
        vh.AddTriangle(0, 5, 1);
    }

#if UNITY_EDITOR
    private void OnGUI()
    {
        if (vertPos.Length == 0) return;

        GUI.color = Color.red;

        for (int i = 0; i < vertPos.Length; ++i)
        {
            var uiPos = new Vector3(vertPos[i].x + 100, Camera.main.pixelHeight - vertPos[i].y - 300, 0);

            GUI.Label(new Rect(uiPos, new Vector2(100, 80)), i.ToString());
        }
    }
#endif
}